<html>
<head>
	<script type="text/javascript">
		var aJaxURL	= "server-side/view/printer_barcode.action.php";		//server side folder url
		var seoyURL	= "server-side/seoy/seoy.action.php";			//server side folder url
		var tName	= "example";									//table name
		var fName	= "add-edit-form";								//form name
		
		$(document).ready(function () {        	
			LoadTable();	
						
			/* Add Button ID, Delete Button ID */
			GetButtons("add_button", "delete_button");
			GetButtons("print_barcode_button", "");
			SetEvents("add_button", "delete_button", "check-all", tName, fName, aJaxURL);
		});

        //SeoYyy
		$(document.body).click(function (e) {
        	$("#barcode_client").autocomplete("close");
        	$("#barcode_printer").autocomplete("close");
        });
		
		function LoadTable(){
			/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
			GetDataTable(tName, aJaxURL, "get_list", 7, "", 0, "");
		}
		
		function LoadDialog(){			
			SeoY("barcode_client", seoyURL, "barcode_client", "", 0);
			SeoY("barcode_printer", seoyURL, "barcode_printer", "", 0);
			
			var id = $("#barcode_id").val();
			if(empty(id)){
				var buttons = {
		            "print": {
		                text: "შენახვა + დაბეჭდვა",
		                id: "print-save-dialog",
		                click: function () {
		                }
		            },
		            "save": {
		                text: "შენახვა",
		                id: "save-dialog",
		                click: function () {
		                }
					}
				};
			}else{				
				var buttons = {
		            "print_s": {
		                text: "სერიულის ბეჭდვა",
		                id: "print-dialog_s",
		                click: function () {
			                //////////////////////////////////////------------------------------------------------
		                }
		            },
		            "print": {
		                text: "დაბეჭდვა",
		                id: "print-dialog",
		                click: function () {
		                }
		            },
		            "save": {
		                text: "შენახვა",
		                id: "save-dialog1",
		                click: function () {
		        			param = new Object();
		        	    	
		                    //Action
		        	    	param.act	= "save_barcode1";	    	
		        		    param.id	= $("#barcode_id").val();   
		        		    param.sn	= $("#serial_number").val();		    
		        		    param.com	= $("#comment").val(); 
		        			if(param.sn == 0){
		        				alert("შეავსეთ სერიული ნომერი!");				
		        			}else{
		        			    $.ajax({
		        			        url: aJaxURL,
		        				    data: param,
		        			        success: function(data) {
		        						if(typeof(data.error) != "undefined"){
		        							if(data.error != ""){
		        								alert(data.error);
		        							}else{
		        								LoadTable();
		        				        		CloseDialog(fName);		        						
		        							}
		        						}
		        				    }
		        			    });
		        			}
			                
		                }
		            },
		            "cancel": {
		                text: "დახურვა",
		                id: "cancel-dialog",
		                click: function () {
		                	$(this).dialog("close");
		                }
					}
				};
				//alert("df");
				$("#barcode_client, #barcode_printer").prop('disabled', true);
				$("#client_btn, #barcode_printer_btn").button('disable');
			}
			
			
			/* Dialog Form Selector Name, Buttons Array */
			GetDialog(fName, 550, "auto", buttons);
		}
		
		
	    // Add - Save
		$(document).on("click", "#save-dialog", function () {
			SaveBarcode(false);
		});

        $(document).on("click", "#print-dialog", function (event) {
        	var id = $("#barcode_id").val();
        	
        	GetPrintPage("[" + id + "]");
		});

        $(document).on("click", "#print-dialog_s", function (event) {
        	var id = $("#barcode_id").val();        	
        	GetPrintPage1("[" + id + "]");
		});
		
        $(document).on("click", "#print-save-dialog", function (event) {
        	SaveBarcode(true);
		});
		
        function SaveBarcode(print){
			param = new Object();
	    	
            //Action
	    	param.act	= "save_barcode";	    	
		    param.id	= $("#barcode_id").val();
		    param.bcl	= $("#barcode_client").val();
		    param.bp	= $("#barcode_printer").val();
		    param.sn	= $("#serial_number").val();		    
		    param.com	= $("#comment").val(); 
		    		    
			if(param.bcl == 0){
				alert("შეავსეთ კლიენტის დასახელება!");
			}else if(param.bp == 0){
				alert("შეავსეთ პრინტერის მოდელი!");
			}else if(param.sn == 0){
				alert("შეავსეთ სერიული ნომერი!");				
			}else{
			    $.ajax({
			        url: aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined"){
							if(data.error != ""){
								alert(data.error);
							}else{
								LoadTable();
				        		CloseDialog(fName);
								if(print){
									GetPrintPage(data.barcode_list);
								}
							}
						}
				    }
			    });
			}
		}
		
        function GetPrintPage(barcode_list){
	    	param = new Object();
	    	
            //Action
	    	param.act	= "get_barcode";
	    	
		    param.list	= barcode_list;
		    
	    	var data = GetAjaxData(param);
	    	var link = GetRootDIR();
	    	
	    	link = link + "server-side/print/print.action.php?" + data;
	    	
	    	var newWin = window.open(link, "JSSite", "width=800,height=800,resizable=yes,scrollbars=yes,status=yes");
	    	newWin.focus();
	    	newWin.onload = function() {
	    		newWin.print();
	    	}
        }
        function GetPrintPage1(barcode_list){
	    	param = new Object();
	    	
            //Action
	    	param.act	= "get_barcode";
	    	
		    param.list	= barcode_list;
		    
	    	var data = GetAjaxData(param);
	    	var link = GetRootDIR();
	    	
	    	link = link + "server-side/print/print1.action.php?" + data;
	    	
	    	var newWin = window.open(link, "JSSite", "width=800,height=800,resizable=yes,scrollbars=yes,status=yes");
	    	newWin.focus();
	    	newWin.onload = function() {
	    		newWin.print();
	    	}
        }
		
        $(document).on("click", ".combobox", function (event) {
	    	var i = $(this).text();
			$("#" + i).autocomplete( "search", "" );
		});

        $(document).on("click", "#barcode_client-widget", function (event) {
        	$("#barcode_printer").focus();          
		});

        $(document).on("click", "#barcode_printer-widget", function (event) {
        	$("#serial_number").focus();           
		});

        $(document).on("keydown", "#print_barodeee", function (event) {
			if (event.keyCode == $.ui.keyCode.ENTER){
				
		    	var act	    = "get_print_barcode";
		    	var list	= $("#print_barodeee").val()
		    	
		    	
		    	var link = GetRootDIR();
		    	
		    	link = link + "server-side/print/print.action.php?act="+act+"&list="+list;
		    	
		    	var newWin = window.open(link, "JSSite", "width=800,height=800,resizable=yes,scrollbars=yes,status=yes");
		    	newWin.focus();
		    	newWin.onload = function() {
		    		newWin.print();
		    	}
		    	$("#print_barodeee").val('');								         	
				event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
            }
		});		
    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
                <h2 align="center">პრინტერის შტრიხ კოდები</h2>   
	        	<div id="button_area">
	        		<button id="add_button">შტრიხ. დამატება</button><button id="delete_button">წაშლა</button>
	        	</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th class="min">მოდელი</th>
                            <th style="width: 100%">კლიენტი</th>
                            <th class="min">სერიული ნომერი</th>
                            <th class="min">შტრიხ კოდი</th>
                            <th class="min">შექმნის თარიღი</th>
                            <th style="width: 100%">შენიშვნა</th>                           
                            <th class="check">#</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_model" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_client" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_serial" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_barcode" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_date" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_comment" value="ფილტრი" class="search_init" />
                            </th>                            
                            <th>
                            	<input type="checkbox" name="check-all" id="check-all">
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="შტრიხ კოდები">
    	<!-- aJax -->
	</div>
</body>
</html>